import { defineStore } from "pinia";
import type { Address } from "@/types/address";
import { addAddressAPI, deleteAddressAPI, editAddressAPI, fetchAddressAPI, findAddressAPI } from "@/api/addressApi";
import { ref } from "vue";


const useAddressStore = defineStore("addressStore", () => {

    const addressList = ref<Address[]>([]);

    /*获取收货地址的请求函数*/
    const fetchAddress = async (): Promise<void> => {
        const { result }: { result: Address[] } = await fetchAddressAPI();
        addressList.value = result;
    };

    /*添加收货地址的请求函数*/
    const addAddress = async (data: Address): Promise<void> => {
        await addAddressAPI(data);
    };

    /*删除收货地址的请求函数*/
    const deleteAddress = async (id: string): Promise<void> => {
        await deleteAddressAPI(id);
    };

    /*查找收货地址详情的请求函数*/
    const findAddress = async (id: string): Promise<Address> => {
        const { result }: { result: Address } = await findAddressAPI(id);
        return result;
    };

    /*修改收货地址的请求函数*/
    const editAddress = async (data: Address): Promise<void> => {
        await editAddressAPI(data);
    };

    return {
        addressList,
        fetchAddress,
        addAddress,
        deleteAddress,
        findAddress,
        editAddress,
    };
});


export default useAddressStore;
